﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StockEngine
{
    public class ListStorage
    {
        private Dictionary<string, List<StockInfo>> stockInfoDictionary = new Dictionary<string, List<StockInfo>>();
        private Dictionary<string, List<StockNameInfo>> stockMarketDictionary;
        private List<DateSpan> myDateSpan;


        public ListStorage()
        {
            //TODO: Some way to get a read on "all" markets and their stocks
            //Maybe it's also not needed for the distinction between NASDAQ, DOW JONES, etc.
            //But for now it's there, even though it doesn't do any difference
            stockMarketDictionary = new Dictionary<string, List<StockNameInfo>>()
            {
                {"NASDAQ", new List<StockNameInfo> { 
                    new StockNameInfo("Autoliv, Inc.", "ALIV-SDB.ST"),
                    new StockNameInfo("HENNES&MAURITZ -B-", "HM-B.ST"),
                    new StockNameInfo("Apple Inc.", "AAPL"),
                    new StockNameInfo("Saab AB", "SAAB-B.ST") }   
                }
            };
            stockMarketDictionary["NASDAQ"].Sort(SetAlphabeticorder);

            myDateSpan = new List<DateSpan> 
            {
                new DateSpan("15 days", new TimeSpan(15, 0, 0, 0)),
                new DateSpan("30 days", new TimeSpan(30, 0, 0, 0)),
                new DateSpan("45 days", new TimeSpan(45, 0, 0, 0)),
                new DateSpan("60 days", new TimeSpan(60, 0, 0, 0)),
                new DateSpan("All", new TimeSpan())
            };

        }

        public Dictionary<string, List<StockNameInfo>> GetStockMarketDictionary()
        {
            return stockMarketDictionary;
        }

        public Dictionary<string, List<StockInfo>> GetStockInfoDictionary()
        {
            return stockInfoDictionary;
        }

        public List<DateSpan> GetDateSpan()
        {
            return myDateSpan;
        }

        public void AddToStockInfoDicationary(string key, List<StockInfo> StockList)
        {
            if (stockInfoDictionary.ContainsKey(key))
            {
                //TODO: Fix so that list's can be updated!
            }
            else
            {
                stockInfoDictionary.Add(key, StockList);
            }
        }

        private int SetAlphabeticorder(StockNameInfo x, StockNameInfo y)
        {
            if (x == null)
            {
                if (y == null)
                    return 0;
                else
                    return -1;
            }
            else
            {
                if (y == null)
                    return 1;
                else
                {
                    int max;
                    if (x.FullName.Length > y.FullName.Length)
                        max = y.FullName.Length;
                    else
                        max = x.FullName.Length;

                    for (int i = 0; i < max; i++)
                    {
                        int diff = x.FullName[i].CompareTo(y.FullName[i]);
                        if (diff != 0)
                            return diff;
                    }
                    return x.FullName.Length.CompareTo(y.FullName.Length);

                }
            }
        }

    }
}
